package com.nakanomay.redis7.redlock.controller;

import lombok.extern.slf4j.Slf4j;
import org.redisson.RedissonMultiLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author nakano_may丶
 * @version 1.0
 * @create 2023-05-09 22:58
 * @description：
 */
@RestController
@Slf4j
public class RedLockController {

    public static final String CACHE_KEY_REDLOCK = "ATGUIGU_REDLOCK";

    @Autowired
    RedissonClient redissonClient1;

    @Autowired
    RedissonClient redissonClient2;

    @Autowired
    RedissonClient redissonClient3;

    boolean isLockBoolean;

    @GetMapping(value = "getmultilock")
    public String getMultiLock()
    {
        String taskThreadID = Thread.currentThread().getId() + "";

        RLock lock1 = redissonClient1.getLock(CACHE_KEY_REDLOCK);
        RLock lock2 = redissonClient2.getLock(CACHE_KEY_REDLOCK);
        RLock lock3 = redissonClient3.getLock(CACHE_KEY_REDLOCK);

        RedissonMultiLock redlock = new RedissonMultiLock(lock1, lock2, lock3);


        redlock.lock();
        try {
            log.info("come in biz multilock:{}" , taskThreadID);
            try {  TimeUnit.SECONDS.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); }
            log.info("task is over multilock:{}" , taskThreadID);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("multilock exception:{}", e.getCause() + "\t" + e.getMessage());
        }
        finally {
            redlock.unlock();
            log.info("释放分布式锁成功，key:{}" + CACHE_KEY_REDLOCK);
        }
        return "multilock task is over:" + taskThreadID;
    }

}
